﻿using IMES.Common.Domain.DariusModel;
using IMES.Common.Dtos;
using IMES.Common.Infrastructure;
using MediatR;

namespace Darius.API.Application.Commands.Order
{
    public class OrderDetailDeleteHandler : IRequestHandler<OrderDetailDeleteCommand, Result>
    {
        private readonly IBaseRepositorys<SaleOrders> saleOrdersRepository;
        private readonly ILogger<OrderDetailDeleteHandler> logger;

        public OrderDetailDeleteHandler(IBaseRepositorys<SaleOrders> saleOrdersRepository, ILogger<OrderDetailDeleteHandler> logger)
        {
            this.saleOrdersRepository = saleOrdersRepository;
            this.logger = logger;
        }
        /// <summary>
        /// 订单删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(OrderDetailDeleteCommand request, CancellationToken cancellationToken)
        {
            try
            {
                var res = 0;
                var result = new Result() { Code = (int)CodeEnum.delete };
                string[] strings=request.SaleOrderIds.Split(',');
                List<SaleOrders> saleOrdersList = new List<SaleOrders>();
                foreach (var item in strings)
                {
                    var list1 = saleOrdersRepository.GetAll().FirstOrDefault(p => p.OrderId == Convert.ToInt32(item));
                    if (list1!= null)
                    {
                        list1.IsDeleted = true;
                         res+=saleOrdersRepository.Update(list1);
                    }
                    
                }
                result.Code = res;
                logger.LogInformation("订单删除成功");
                return result;
                
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "订单删除失败");
                return new Result() { Code = (int)CodeEnum.error, Messege = "订单删除失败" };
            }
        }


    }
}
